package top.soulblack.purchasesale.controller.setting.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.model.SystemAnnounce;
import top.soulblack.purchasesale.service.system.AnnounceService;

import static top.soulblack.purchasesale.common.CodeMsg.ANNOUNCE_DELETE;
import static top.soulblack.purchasesale.common.CodeMsg.ANNOUNCE_EDIT_ERROR;
import static top.soulblack.purchasesale.common.CodeMsg.ANNOUNCE_EXIST;

/**
 * create by 小川
 */
@Controller
@RequestMapping("/announce")
public class AnnounceController {

    @Autowired
    private AnnounceService announceService;

    @ResponseBody
    @RequestMapping("/addAnnounce")
    public Result<Boolean> addAnnounce(SystemAnnounce systemAnnounce) {
        if (announceService.existAnnounce(systemAnnounce.getTitle())) {
            return Result.error(ANNOUNCE_EXIST);
        }
        announceService.addAnnounce(systemAnnounce);
        return Result.success(true);
    }

    @ResponseBody
    @RequestMapping("/editAnnounce")
    public Result<Boolean> editAnnounce(SystemAnnounce systemAnnounce) {
        System.out.println(systemAnnounce.getId());
        if (announceService.updateAnnounce(systemAnnounce)) {
            return Result.success(true);
        }
        return Result.error(ANNOUNCE_DELETE);
    }

    @ResponseBody
    @RequestMapping("/deleteAnnounce")
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> deleteAnnounce(int announceId) {
        System.out.println(announceId);
        if (announceService.deleteAnnounce(announceId)) {
            return Result.success(true);
        }
        return Result.error(ANNOUNCE_EDIT_ERROR);
    }

}
